Completed
Pull Request — master (#195)
by Sander
04:09 queued 27s
created

angular.controller(ꞌVaultCtrlꞌ)   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 175

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 4
nop 9
dl 0
loc 175
rs 8.2857
c 1
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
	/**
27
	 * @ngdoc function
28
	 * @name passmanApp.controller:MainCtrl
29
	 * @description
30
	 * # MainCtrl
31
	 * Controller of the passmanApp
32
	 */
33
	angular.module('passmanApp')
34
		.controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', '$translate', '$rootScope', function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope) {
35
			VaultService.getVaults().then(function (vaults) {
36
				$scope.vaults = vaults;
37
				if (SettingsService.getSetting('defaultVault') != null) {
0 ignored issues
show
Coding Style introduced by
It is recommended to use !== to compare with null.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
38
					var default_vault = SettingsService.getSetting('defaultVault');
39
40
					/**
41
					 * Using a native for loop for preformance reasons.
42
					 * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
43
					 */
44
					for (var i = 0; i < vaults.length; i++) {
45
						var vault = vaults[i];
46
						if (vault.guid === default_vault.guid) {
47
							$scope.default_vault = true;
48
							$scope.list_selected_vault = vault;
49
							SettingsService.setSetting('defaultVault', vault);
50
							if (SettingsService.getSetting('defaultVaultPass')) {
51
								$location.path('/vault/' + vault.guid);
52
							}
53
							break;
54
						}
55
					}
56
				}
57
			});
58
59
60
			var key_strengths = [
61
				'password.poor',
62
				'password.poor',
63
				'password.weak',
64
				'password.good',
65
				'password.strong'
66
			];
67
68
			$scope.default_vault = false;
69
			$scope.remember_vault_password = false;
70
			$scope.auto_logout_timer = false;
71
			$scope.logout_timer = '0';
72
			$scope.list_selected_vault = false;
73
			$scope.minimal_value_key_strength = 3;
74
75
			var settingsLoaded = function () {
76
				$scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength');
77
				$translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function(translation){
78
					$scope.required_score = {'strength': translation};
79
				});
80
			};
81
82
			if(!SettingsService.getSetting('settings_loaded')){
83
				$rootScope.$on('settings_loaded', function () {
84
					settingsLoaded();
85
				});
86
			} else {
87
				settingsLoaded();
88
			}
89
90
			$scope.toggleDefaultVault = function () {
91
				$scope.default_vault = !$scope.default_vault;
92
				if ($scope.default_vault === true) {
93
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
94
				} else {
95
					SettingsService.setSetting('defaultVault', null);
96
				}
97
			};
98
99
			$scope.toggleRememberPassword = function () {
100
				$scope.remember_vault_password = !$scope.remember_vault_password;
101
				if ($scope.remember_vault_password) {
102
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
103
					$scope.default_vault = true;
104
				}
105
				if ($scope.remember_vault_password !== true) {
106
					SettingsService.setSetting('defaultVault', null);
107
				}
108
			};
109
110
			$scope.toggleAutoLogout = function(){
111
				$scope.auto_logout_timer = !$scope.auto_logout_timer;
112
			};
113
114
			$scope.clearState = function () {
115
				$scope.list_selected_vault = false;
116
				$scope.creating_vault = false;
117
				$scope.error = false;
118
			};
119
120
			$scope.selectVault = function (vault) {
121
				$scope.list_selected_vault = vault;
122
			};
123
			$scope.sharing_keys = {};
124
			$scope.newVault = function () {
125
				$scope.creating_vault = true;
126
				var key_size = 1024;
127
				ShareService.generateRSAKeys(key_size).progress(function (progress) {
128
					var p = progress > 0 ? 2 : 1;
129
					var msg = $translate.instant('generating.sharing.keys');
130
					msg = msg.replace('%step', p);
131
					$scope.creating_keys = msg;
132
					$scope.$digest();
133
				}).then(function (kp) {
134
					var pem = ShareService.rsaKeyPairToPEM(kp);
135
					$scope.creating_keys = false;
136
					$scope.sharing_keys.private_sharing_key = pem.privateKey;
137
					$scope.sharing_keys.public_sharing_key = pem.publicKey;
138
					$scope.$digest();
139
				});
140
141
			};
142
143
			var _loginToVault = function (vault, vault_key) {
144
				var _vault = angular.copy(vault);
145
				_vault.vaultKey = angular.copy(vault_key);
146
				delete _vault.credentials;
147
				var timer =  parseInt($scope.logout_timer);
148
				if($scope.auto_logout_timer && timer > 0 ){
149
					$rootScope.$broadcast('logout_timer_set', timer*60);
150
				}
151
152
				VaultService.setActiveVault(_vault);
153
				$location.path('/vault/' + vault.guid);
154
			};
155
156
			$scope.selectLogoutTimer = function (time) {
157
				$scope.auto_logout_timer = true;
158
				$scope.logout_timer = time;
159
			};
160
161
			$scope.vaultDecryptionKey = '';
162
			$scope.loginToVault = function (vault, vault_key) {
163
				$scope.error = false;
164
				var _vault = angular.copy(vault);
165
				_vault.vaultKey = angular.copy(vault_key);
166
167
				VaultService.setActiveVault(_vault);
168
				try {
169
					EncryptService.decryptString(vault.challenge_password);
170
					if ($scope.remember_vault_password) {
171
						SettingsService.setSetting('defaultVaultPass', vault_key);
172
					}
173
					_loginToVault(vault, vault_key);
174
175
				} catch (e) {
176
					$scope.error = $translate.instant('invalid.vault.key');
177
				}
178
179
			};
180
181
182
			$scope.createVault = function (vault_name, vault_key, vault_key2) {
183
				if (vault_key !== vault_key2) {
184
					$scope.error = $translate.instant('password.do.not.match');
185
					return;
186
				}
187
				VaultService.createVault(vault_name).then(function (vault) {
188
					$scope.vaults.push(vault);
189
					var _vault = angular.copy(vault);
190
					_vault.vaultKey = angular.copy(vault_key);
191
					VaultService.setActiveVault(_vault);
192
					SettingsService.setSetting('defaultVaultPass', null);
193
					SettingsService.setSetting('defaultVault', null);
194
					var test_credential = CredentialService.newCredential();
195
					test_credential.label = 'Test key for vault ' + vault_name;
196
					test_credential.hidden = true;
197
					test_credential.vault_id = vault.vault_id;
198
					test_credential.password = 'lorum ipsum';
199
					CredentialService.createCredential(test_credential).then(function () {
200
						_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
201
						_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
202
						VaultService.updateSharingKeys(_vault).then(function () {
203
							_loginToVault(vault, vault_key);
204
						});
205
					});
206
				});
207
			};
208
		}]);
209
}());